package club.justwill.single.model;

import club.justwill.single.helper.CoreHelper;
import club.justwill.single.impl.ToolsImpl;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;

import java.io.Serializable;
import java.util.Date;
import java.util.UUID;

@Component(SessionState.BEAN)
@Scope(WebApplicationContext.SCOPE_SESSION)
public class SessionState implements Serializable {
	private static final long serialVersionUID = 8592044459979045710L;

	private String cookieId;

	public static final String BEAN = "sessionState";

	private static final int ALLOWED_FAILED_ATTEMPT = 3;

	private String sessionId = UUID.randomUUID().toString();

	private long userId = UserType.ANONYMOUS;

	private int userType = UserType.ANONYMOUS;

	private int failedAttempt = ToolsImpl.ZERO;

	private String userName = "";

	private Date verificationExpiry = null;

	private int authCode = 0;

	private long expire;

	private String mobile;

	private long orderId;

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public boolean isLoggedIn() {
		return userType != UserType.ANONYMOUS;
	}

	public boolean isConsumer() {
		return CoreHelper.isConsumer(getUserType());
	}

	public boolean isServiceProvider() {
		return CoreHelper.isServiceProvider(getUserType());
	}

	public boolean isTestCaptcha() {
		return failedAttempt >= ALLOWED_FAILED_ATTEMPT;
	}

	public boolean isActiveVerificationCode() {
		return verificationExpiry != null && verificationExpiry.after(new Date());
	}

	public int getVerificationCountDown() {
		int countdown;
		if (verificationExpiry == null) {
			countdown = ToolsImpl.ZERO;
		} else {
			countdown = (int) ((verificationExpiry.getTime() - (new Date()).getTime()) / 1000);
			if (countdown <= ToolsImpl.ZERO) {
				countdown = ToolsImpl.ZERO;
			}
		}
		return countdown;
	}

	public void setVerificationExpiry(Date expiry) {
		verificationExpiry = expiry;
	}

	public void incrementFailedAttempt() {
		failedAttempt++;
	}

	public void resetFailedAttempt() {
		failedAttempt = ToolsImpl.ZERO;
	}

	public  String getSessionId() {
		return sessionId;
	}

	public void setSessionId(String sessionId) {
		this.sessionId = sessionId;
	}

	protected int getFailedAttempt() {
		return failedAttempt;
	}

	protected void setFailedAttempt(int failedAttempt) {
		this.failedAttempt = failedAttempt;
	}
	//generated getters and setters

	public long getUserId() {
		return userId;
	}

	public void setUserId(long userId) {
		this.userId = userId;
	}

	public int getUserType() {
		return userType;
	}

	public void setUserType(int userType) {
		this.userType = userType;
	}

	public int getAuthCode() {
		return authCode;
	}

	public void setAuthCode(int authCode) {
		this.authCode = authCode;
	}

	public long getExpire() {
		return expire;
	}

	public void setExpire(long expire) {
		this.expire = expire;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public Boolean isAutoCodeExpired() { return System.currentTimeMillis() > expire; }

	public long getOrderId() {
		return orderId;
	}

	public void setOrderId(long orderId) {
		this.orderId = orderId;
	}

	public String getCookieId() {
		return cookieId;
	}

	public void setCookieId(String cookieId) {
		this.cookieId = cookieId;
	}
}
